#include <string.h>
#include "main.h"
#include "KeyBoard_Driver.h"

#include "oled.h"
#include "usart.h"

uint8_t Press_Num = 0;

uint8_t KeyControlByte5 = 0x00;

uint8_t myKeyBoard_KeyIsControl = 1;
uint8_t myKeyBoard_KeyStateChangedFlag = 0;
uint8_t g_myKeyBoard_DataWaitForUploadFlag = 0;

KeyState_enumTypedef myKeyBoard_KeyState[6][5] = {KEYUNPRESSED};
KeyState_enumTypedef myKeyBoard_KeyState_Pre[6][5] = {KEYUNPRESSED};
KeyState_enumTypedef LCD_KeyState[6][5] = {KEYUNPRESSED};

AT_KEYDATA_Structure_Typedef ATKeyData[6];

unsigned char BTK05_ATKeyDataPack[8] =
{
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00
};

const uint16_t key_Col_Pin[5] =
{
        COL1_Pin,
        COL2_Pin,
        COL3_Pin,
        COL4_Pin,
        COL5_Pin,
};

const char * myKeyBoard_KeyMap[6][5] =
{
        "   =   ", " Line- "," Line+ "," WBS<- "," WBS-> ",
        " PASTE ", " COPY ", " ESC ",  "  F2  ",   "   S   ",
        "   7   ", "   8   ","   9   ",  "   F   ","  <-  ",
        "Delete",  "   4   ","   5   ",  "   6   ","   -   ",
        "   1   ", "   2   ","   3   ",  "   +   ","   ,   ",
        "   0   ", "   .   "," Enter ",  "   ^   ","   *   "
};

const char * Project_KeyMap[9] = {"LCtrl","LShift","C","V","LAlt","F5","F6","Left","Right",};
const unsigned char Project_KeyMap_Value[9] = {xxCK,xxCK,0x06,0x19,xxCK,0x3E,0x3F,0x50,0x4F};

KeyState_enumTypedef Project_KeyState[9] = {KEYUNPRESSED};
KeyState_enumTypedef Project_KeyState_Pre[9] = {KEYUNPRESSED};

const unsigned char myKeyBoard_KeyMap_Value_LeftHand[6][5] =
{
        xxCK,0x29,0x3A,0x3B,0x3C,
        xxCK,0x2B,0x1E,0x1F,0x20,
        0x14,0x1A,0x08,0x15,0x17,
        xxCK,0x04,0x16,0x07,0x09,
        0x1D,0x1B,0x06,0x19,0x05,
        0x2C,0x36,0x28,xxSK,0x00
};

const unsigned char myKeyBoard_KeyMap_Value_Project[6][5] =
{
        xxSK,xxSK,xxSK,xxSK,xxSK,
        xxCK,xxSK,0x29,0x3B,0x16,
        0x24,0x25,0x26,0x09,0x2A,
        0x4C,0x21,0x22,0x23,0x2D,
        0x1E,0x1F,0x20,0x2E,0x36,
        0x27,0x37,0x28,xxSK,0x00
};

void Switch_Mode()
{
  if(HAL_GPIO_ReadPin(KEY_MODE_1_GPIO_Port,KEY_MODE_1_Pin) == GPIO_PIN_SET)
  {
    Key_Board_State = Left_Hand_Mode;
  }
  else if(HAL_GPIO_ReadPin(KEY_MODE_2_GPIO_Port,KEY_MODE_2_Pin) == GPIO_PIN_SET)
  {
    Key_Board_State = Project_Mode;
  }
  if(Key_Board_State_Pre != Key_Board_State)
  {
    if(Key_Board_State == Left_Hand_Mode)
    {
      //LCD_ShowString(0,0,(uint8_t *)" Left Hand Mode",WHITE,BLACK,16,0);
      OLED_ShowString(0,0,(uint8_t *)" Left Hand Mode",16,1);
      OLED_Refresh();
    }
    else if(Key_Board_State == Project_Mode)
    {
      //LCD_ShowString(0,0,(uint8_t *)" Project   Mode",WHITE,BLACK,16,0);
      OLED_ShowString(0,0,(uint8_t *)" Project   Mode",16,1);
      OLED_Refresh();
    }
  }
  Key_Board_State_Pre = Key_Board_State;
}

void myKeyBoard_ScanKeyAndUpdataATBuffer()
{
  myKeyBoard_KeyScan();//扫描键盘矩阵
  myKeyBoard_JudgeKeyStateChange();//判断这一次和上一次有没有变化
  if(myKeyBoard_KeyStateChangedFlag)//如果有变化
  {
    myKeyBoard_KeyStateChangedFlag = 0;
    myKeyBoard_UpdataATDataPack();
    g_myKeyBoard_DataWaitForUploadFlag = 1;//数据上传完成
  }
}

void myKeyBoard_KeyScan()//扫描键盘
{
  for(int i = 0;i < 5;i++)
  {
    myKeyBoard_KeyScan_rowScan(GPIOA,i,key_Col_Pin);
  }
  for(int i = 0;i < 9;i++)
  {
    Project_KeyState[i] = KEYUNPRESSED;
  }
  Hong_Function();
}

void myKeyBoard_JudgeKeyStateChange()
{
  uint8_t i;
  uint8_t j;
  for(i = 0;i < 6;i++)//检查键盘状态是否发生改变
  {
    for(j = 0;j < 5;j++)
    {
      if(myKeyBoard_KeyState_Pre[i][j] != myKeyBoard_KeyState[i][j])//如果不一样，退出循环
      {
        myKeyBoard_KeyStateChangedFlag = 1;
        break;
      }
    }
    if(myKeyBoard_KeyStateChangedFlag)
    {
      break;
    }
  }

  for(i = 0;i < 6;i++)//将这次的状态传递到上次
  {
    for(j = 0;j < 5;j++)
    {
      myKeyBoard_KeyState_Pre[i][j] = myKeyBoard_KeyState[i][j];
    }
  }

  for(i = 0;i < 9;i++)//检查状态是否发生变化
  {
    if(Project_KeyState_Pre[i] != Project_KeyState[i])//如果发生了变化
    {
      myKeyBoard_KeyStateChangedFlag = 1;
      break;
    }
  }
  for(i = 0;i < 9;i++)//把这次的状态传递到上次
  {
    Project_KeyState_Pre[i] = Project_KeyState[i];
  }

}

void Hong_Function()
{
  if(Key_Board_State == Left_Hand_Mode)
  {
    if(myKeyBoard_KeyState[5][3] == KEYPRESSED)
    {
      myKeyBoard_KeyState[3][0] = KEYPRESSED;
      myKeyBoard_KeyState[4][3] = KEYPRESSED;
    }
  }
  else if(Key_Board_State == Project_Mode)
  {
    if(myKeyBoard_KeyState[0][0] == KEYPRESSED)
    {
      Project_KeyState[0] = KEYPRESSED;
      Project_KeyState[1] = KEYPRESSED;
      Project_KeyState[5] = KEYPRESSED;
    }
    else if(myKeyBoard_KeyState[0][1] == KEYPRESSED)
    {
      Project_KeyState[0] = KEYPRESSED;
      myKeyBoard_KeyState[3][4] = KEYPRESSED;
    }
    else if(myKeyBoard_KeyState[0][2] == KEYPRESSED)
    {
      Project_KeyState[0] = KEYPRESSED;
      myKeyBoard_KeyState[4][3] = KEYPRESSED;
    }
    else if(myKeyBoard_KeyState[0][3] == KEYPRESSED)
    {
      Project_KeyState[1] = KEYPRESSED;
      Project_KeyState[4] = KEYPRESSED;
      Project_KeyState[7] = KEYPRESSED;
    }
    else if(myKeyBoard_KeyState[0][4] == KEYPRESSED)
    {
      Project_KeyState[1] = KEYPRESSED;
      Project_KeyState[4] = KEYPRESSED;
      Project_KeyState[8] = KEYPRESSED;
    }
    else if(myKeyBoard_KeyState[1][0] == KEYPRESSED)
    {
      Project_KeyState[0] = KEYPRESSED;
      Project_KeyState[3] = KEYPRESSED;
    }
    else if(myKeyBoard_KeyState[1][1] == KEYPRESSED)
    {
      Project_KeyState[0] = KEYPRESSED;
      Project_KeyState[2] = KEYPRESSED;
    }
    if(myKeyBoard_KeyState[5][3] == KEYPRESSED)
    {
      Project_KeyState[0] = KEYPRESSED;
      Project_KeyState[1] = KEYPRESSED;
      Project_KeyState[6] = KEYPRESSED;
    }

  }
}

void myKeyBoard_KeyScan_rowScan(GPIO_TypeDef * col_GPIOPort, uint8_t col_GPIOPin_Index,const uint16_t * p_col_GPIOPin)
{

  HAL_GPIO_WritePin(col_GPIOPort,*(p_col_GPIOPin + col_GPIOPin_Index),GPIO_PIN_SET);//写高电平

  if(HAL_GPIO_ReadPin(ROW1_GPIO_Port,ROW1_Pin) == GPIO_PIN_SET)//第一行扫描
  {
    HAL_Delay(5);
    if(HAL_GPIO_ReadPin(ROW1_GPIO_Port,ROW1_Pin) == GPIO_PIN_SET)
    {
      myKeyBoard_KeyState[0][col_GPIOPin_Index] = KEYPRESSED;
      LCD_KeyState[0][col_GPIOPin_Index] = KEYPRESSED;
    }
  }
  else
  {
    myKeyBoard_KeyState[0][col_GPIOPin_Index] = KEYUNPRESSED;
    LCD_KeyState[0][col_GPIOPin_Index] = KEYUNPRESSED;
  }

  if(HAL_GPIO_ReadPin(ROW2_GPIO_Port,ROW2_Pin) == GPIO_PIN_SET)//第二行扫描
  {
    HAL_Delay(5);
    if(HAL_GPIO_ReadPin(ROW2_GPIO_Port,ROW2_Pin) == GPIO_PIN_SET)
    {
      myKeyBoard_KeyState[1][col_GPIOPin_Index] = KEYPRESSED;
      LCD_KeyState[1][col_GPIOPin_Index] = KEYPRESSED;
    }
  }
  else
  {
    myKeyBoard_KeyState[1][col_GPIOPin_Index] = KEYUNPRESSED;
    LCD_KeyState[1][col_GPIOPin_Index] = KEYUNPRESSED;
  }

  if(HAL_GPIO_ReadPin(ROW3_GPIO_Port,ROW3_Pin) == GPIO_PIN_SET)//第三行扫描
  {
    HAL_Delay(5);
    if(HAL_GPIO_ReadPin(ROW3_GPIO_Port,ROW3_Pin) == GPIO_PIN_SET)
    {
      myKeyBoard_KeyState[2][col_GPIOPin_Index] = KEYPRESSED;
      LCD_KeyState[2][col_GPIOPin_Index] = KEYPRESSED;
    }
  }
  else
  {
    myKeyBoard_KeyState[2][col_GPIOPin_Index] = KEYUNPRESSED;
    LCD_KeyState[2][col_GPIOPin_Index] = KEYUNPRESSED;
  }

  if(HAL_GPIO_ReadPin(ROW4_GPIO_Port,ROW4_Pin) == GPIO_PIN_SET)//第四行扫描
  {
    HAL_Delay(5);
    if(HAL_GPIO_ReadPin(ROW4_GPIO_Port,ROW4_Pin) == GPIO_PIN_SET)
    {
      myKeyBoard_KeyState[3][col_GPIOPin_Index] = KEYPRESSED;
      LCD_KeyState[3][col_GPIOPin_Index] = KEYPRESSED;
    }
  }
  else
  {
    myKeyBoard_KeyState[3][col_GPIOPin_Index] = KEYUNPRESSED;
    LCD_KeyState[3][col_GPIOPin_Index] = KEYUNPRESSED;
  }

  if(HAL_GPIO_ReadPin(ROW5_GPIO_Port,ROW5_Pin) == GPIO_PIN_SET)//第五行扫描
  {
    HAL_Delay(5);
    if(HAL_GPIO_ReadPin(ROW5_GPIO_Port,ROW5_Pin) == GPIO_PIN_SET)
    {
      myKeyBoard_KeyState[4][col_GPIOPin_Index] = KEYPRESSED;
      LCD_KeyState[4][col_GPIOPin_Index] = KEYPRESSED;
    }
  }
  else
  {
    myKeyBoard_KeyState[4][col_GPIOPin_Index] = KEYUNPRESSED;
    LCD_KeyState[4][col_GPIOPin_Index] = KEYUNPRESSED;
  }

  if(HAL_GPIO_ReadPin(ROW6_GPIO_Port,ROW6_Pin) == GPIO_PIN_SET)//第五行扫描
  {
    HAL_Delay(5);
    if(HAL_GPIO_ReadPin(ROW6_GPIO_Port,ROW6_Pin) == GPIO_PIN_SET)
    {
      myKeyBoard_KeyState[5][col_GPIOPin_Index] = KEYPRESSED;
      LCD_KeyState[5][col_GPIOPin_Index] = KEYPRESSED;
    }
  }
  else
  {
    myKeyBoard_KeyState[5][col_GPIOPin_Index] = KEYUNPRESSED;
    LCD_KeyState[5][col_GPIOPin_Index] = KEYUNPRESSED;
  }

  HAL_GPIO_WritePin(col_GPIOPort,*(p_col_GPIOPin + col_GPIOPin_Index),GPIO_PIN_RESET);
}



void myKeyBoard_UpdataATDataPack()
{
  uint8_t i;
  uint8_t j;
  uint8_t k;
  uint8_t AlreadyExistFlag = 0;
  KeyControlByte5 = 0x00;

  //检测上次按下的按键是否还在继续按，如果没再继续按了就将ATKeyData的数据清除
  for(i = 0;i < 6;i++)
  {
    if(ATKeyData[i].ATKeyData != 0x00)//找出不是空的元素
    {
      if(myKeyBoard_KeyState[ATKeyData[i].ROW_N][ATKeyData[i].COL_N] == KEYUNPRESSED)//判断这个元素是不是还在按
      {
        ATKeyData[i].ATKeyData = 0x00;
      }
      if(Project_KeyState[ATKeyData[i].Index] == KEYUNPRESSED)
      {
        ATKeyData[i].ATKeyData = 0x00;
      }
    }
  }

  if(Key_Board_State == Project_Mode)
  {
    for(i = 0;i < 9;i++)
    {
      if(Project_KeyState[i] == KEYPRESSED)//如果按键被按下
      {
        myKeyBoard_ControlKeyProcess_Project(i);//检查这个按键是不是控制按键
      }
      if(myKeyBoard_KeyIsControl == 0)//如果不是
      {
        for(k = 0;k < 6;k++)
        {
          if(ATKeyData[k].ATKeyData == Project_KeyMap_Value[i])
          {
            AlreadyExistFlag = 1;
            break;
          }
        }
        if(AlreadyExistFlag == 0)
        {
          for(k = 0;k<6;k++)
          {
            if(ATKeyData[k].ATKeyData == 0x00)
            {
              ATKeyData[k].ATKeyData = Project_KeyMap_Value[i];
              ATKeyData[k].Index = i;
              break;
            }
          }
        }
        else//如果这个按键已经存在，则不管它。
        {
          AlreadyExistFlag = 0;
        }
        myKeyBoard_KeyIsControl = 1;
      }
    }
  }

  for(i = 0;i < 6;i++)
  {
    for(j = 0;j < 5;j++)
    {
      if(myKeyBoard_KeyState[i][j] == KEYPRESSED)//如果按键被按下则处理键盘数据包
      {
        if(Key_Board_State == Left_Hand_Mode)
        {
          myKeyBoard_ControlKeyProcess_LeftHand(i,j);//判断控制按键是否被按下
        }
        else
        {
          myKeyBoard_KeyIsControl = 0;
        }
        if(myKeyBoard_KeyIsControl == 0)//如果控制按键没被按下
        {
          for(k = 0;k<6;k++)
          {
            if(Key_Board_State == Left_Hand_Mode)
            {
              if(ATKeyData[k].ATKeyData == myKeyBoard_KeyMap_Value_LeftHand[i][j])//判断这个按键是否已经被按下
              {
                AlreadyExistFlag = 1;
                break;
              }
            }
            else if(Key_Board_State == Project_Mode)
            {
              if(ATKeyData[k].ATKeyData == myKeyBoard_KeyMap_Value_Project[i][j])//判断这个按键是否已经被按下
              {
                AlreadyExistFlag = 1;
                break;
              }
            }
          }
          if(AlreadyExistFlag == 0)//如果数据包中原本没有这个按键，则放入
          {
            //判断有没有空位，有空位就放进去
            for(k = 0;k < 6;k++)
            {
              if(ATKeyData[k].ATKeyData == 0x00)
              {
                if(Key_Board_State == Left_Hand_Mode)
                {
                  ATKeyData[k].ATKeyData = myKeyBoard_KeyMap_Value_LeftHand[i][j];
                }
                else if(Key_Board_State == Project_Mode)
                {
                  ATKeyData[k].ATKeyData = myKeyBoard_KeyMap_Value_Project[i][j];
                }
                ATKeyData[k].ROW_N = i;
                ATKeyData[k].COL_N = j;
                break;
              }
            }
          }
          else//如果这个按键已经存在，则不管它。
          {
            AlreadyExistFlag = 0;
          }
          myKeyBoard_KeyIsControl = 1;//控制按键标志位复位
        }
      }
    }
  }

  BTK05_ATKeyDataPack[0] = KeyControlByte5;//更新控制按键字节

  for(k = 0; k < 6; k++)
  {
    BTK05_ATKeyDataPack[2 + k] = ATKeyData[k].ATKeyData;//更新普通按键字节
  }
}

void myKeyBoard_ControlKeyProcess_LeftHand(uint8_t rowi,uint8_t colj)
{
  if(strcmp(myKeyBoard_KeyMap[rowi][colj],"PASTE") == 0)
  {
    KeyControlByte5 = KeyControlByte5 | (0x01 << 1);
  }
  else if(strcmp(myKeyBoard_KeyMap[rowi][colj],"RShift") == 0)
  {
    KeyControlByte5 = KeyControlByte5 | (0x01 << 5);
  }
  else if(strcmp(myKeyBoard_KeyMap[rowi][colj],"Delete") == 0)
  {
    KeyControlByte5 = KeyControlByte5 | (0x01 << 0);
  }
  else if(strcmp(myKeyBoard_KeyMap[rowi][colj],"RCtrl") == 0)
  {
    KeyControlByte5 = KeyControlByte5 | (0x01 << 4);
  }
  else if(strcmp(myKeyBoard_KeyMap[rowi][colj],"=") == 0)
  {
    KeyControlByte5 = KeyControlByte5 | (0x01 << 2);
  }
  else if(strcmp(myKeyBoard_KeyMap[rowi][colj],"RAlt") == 0)
  {
    KeyControlByte5 = KeyControlByte5 | (0x01 << 6);
  }
  else if(strcmp(myKeyBoard_KeyMap[rowi][colj],"Win") == 0)
  {
    KeyControlByte5 = KeyControlByte5 | (0x01 << 3);
  }
  else
  {
    myKeyBoard_KeyIsControl = 0;
  }
}

void myKeyBoard_ControlKeyProcess_Project(uint8_t index)
{
  if(strcmp(Project_KeyMap[index],"LShift") == 0)
  {
    KeyControlByte5 = KeyControlByte5 | (0x01 << 1);
  }
  else if(strcmp(Project_KeyMap[index],"LCtrl") == 0)
  {
    KeyControlByte5 = KeyControlByte5 | (0x01 << 0);
  }
  else if(strcmp(Project_KeyMap[index],"LAlt") == 0)
  {
    KeyControlByte5 = KeyControlByte5 | (0x01 << 2);
  }
  else
  {
    myKeyBoard_KeyIsControl = 0;
  }
}

